<?php namespace App\Controllers\Manager;

//http://www.codeforest.net/laravel4-simple-website-with-backend-1
 
use App\Models\Project;
use App\Models\ProjectImage;
use App\Models\ProjectFile;
use Input, Redirect, Sentry, Str, Response, Config;
use Notification;
 
class ProjectsController extends \BaseController {
 
    public function index()
    {
        return \View::make('manager.projects.index')
            ->with('projects', Project::all());
    }
 
    public function show($id)
    {
        /*return \View::make('manager.pages.show')
            ->with('page', Page::find($id));*/
    }
 
    public function create()
    {
        return \View::make('manager.projects.create');
    }
 
    public function store()
    {
        $project = new Project;
        $project->title   = Input::get('title');
        $project->slug    = Str::slug(Input::get('slug'));
        $project->body   = Input::get('body');
        $project->save();
        
        Notification::success('The project was saved.');
     
        return Redirect::route('manager.projects.index');
    }
 
    public function edit($id)
    {
        $project = Project::find($id);
		
		return \View::make('manager.projects.edit')
            ->with('project', $project);
    }
 
    public function update($id)
    {
        $project = Project::find($id);
        $project->title   = Input::get('title');
        $project->slug    = Str::slug(Input::get('slug'));
        $project->body   = Input::get('body');
        
		$project->save();
		
		Notification::success('The project was saved.');
     
        return Redirect::route('manager.projects.edit', $project->id);
    }
 
    public function destroy($id)
    {
        $project = Project::find($id);
        $project->delete();

        Notification::info('The project was deleted.');

        return Redirect::route('manager.projects.index');
    }

    public function getSlug()
    {
        return Response::json(array(
            "val" => Str::slug(Input::get("str"))
        ));
    }
	
	public function uploadImage($id)
	{
		$project = Project::find($id);
		
		if (Input::hasFile('theimage'))
		{
			$destinationPath = Config::get('app.projectimagepath').$id;
			$path_parts = pathinfo(Input::file('theimage')->getClientOriginalName());
			
			$newfilename = Str::slug($path_parts["filename"]).".".$path_parts["extension"];
			
			if(!file_exists($destinationPath."/".$newfilename))
			{
				Input::file('theimage')->move($destinationPath, $newfilename);
			
				$pi = new ProjectImage;
				$pi->imagefilename = $newfilename;
				$pi->save();
				
				$project->images()->attach($pi->id);
			}
			
			return Response::json(array(
				"error" => 0 //Str::slug(Input::get("str"))
			));
		}
		else
		{
			return Response::json(array(
				"error" => "No image specified" //Str::slug(Input::get("str"))
			));
		}
	}
	
	public function getImages($projectid)
	{
		return \View::make('manager.projects.images')
            ->with('project', Project::find($projectid));
	}
	
	public function deleteImage($projectid, $imageid)
	{
		$project = Project::find($projectid);
		$image = ProjectImage::find($imageid);
		
		$project->images()->detach($image->id);
		if(file_exists(Config::get('app.projectimagepath').$projectid."/".$image->imagefilename))
		{
			unlink(Config::get('app.projectimagepath').$projectid."/".$image->imagefilename);
		}
		$image->delete();
	}
	
	public function uploadFile($id)
	{
		$project = Project::find($id);
		
		if (Input::hasFile('thefile'))
		{
			$destinationPath = Config::get('app.projectfilepath').$id;
			$path_parts = pathinfo(Input::file('thefile')->getClientOriginalName());
			
			$newfilename = Str::slug($path_parts["filename"]).".".$path_parts["extension"];
			
			if(!file_exists($destinationPath."/".$newfilename))
			{
				Input::file('thefile')->move($destinationPath, $newfilename);
			
				$pf = new ProjectFile;
				$pf->filename = $newfilename;
				$pf->save();
				
				$project->files()->attach($pf->id);
			}
			
			return Response::json(array(
				"error" => 0 //Str::slug(Input::get("str"))
			));
		}
		else
		{
			return Response::json(array(
				"error" => "No file specified" //Str::slug(Input::get("str"))
			));
		}
	}
	
	public function getFiles($projectid)
	{
		return \View::make('manager.projects.files')
            ->with('project', Project::find($projectid));
	}
	
	public function deleteFile($projectid, $fileid)
	{
		$project = Project::find($projectid);
		$file = ProjectFile::find($fileid);
		
		$project->files()->detach($file->id);
		if(file_exists(Config::get('app.projectfilepath').$projectid."/".$file->filename))
		{
			unlink(Config::get('app.projectfilepath').$projectid."/".$file->filename);
		}
		$file->delete();
	}
}